<template>
  <div>
    <van-cell-group border>
      <van-field
        v-model="tel"
        label="手机号"
        placeholder="请输入手机号.."
        autofocus
      />
    </van-cell-group>
    <div class="loginBtn">
      <van-button type="danger" round block :disabled="flag" @click="next"
        >下一步</van-button
      >
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import { Dialog } from "vant";
Vue.use(Dialog);
import { GETUserTel } from "@/request/api";

import { Toast } from 'vant';
Vue.use(Toast);
export default {
  data() {
    return {
      tel: "",
      //   flag:true,// flag是不是需要依赖我们的 tel来计算它的true和false
    };
  },
  computed: {
    flag() {
      // 满足输入的内容满足手机号的正则规则的
      if (/^(?:(?:\+|00)86)?1\d{10}$/.test(this.tel)) {
        //输入的内容符合正则规则
        return false; // 显示的红色可用
      } else {
        return true; //灰红色 禁用
      }
    },
  },

  methods: {
    next() {
      // 15117136429
      GETUserTel({ tel: this.tel }).then((res) => {
        console.log(res);
        if (res.data.code == 10005) {
          Dialog.confirm({
            message: "用户名已存在,是否去登陆",
          })
            .then(() => {
              // on confirm
              console.log("点了确定");
              this.$router.back();
            })
            .catch(() => {
              console.log("点了取消");
            });
          // 
          //   注册15117136424存密码：Abc123
        } else if (res.data.code == 200) {
          // 在localstorage中进行存储tel
          localStorage.setItem("tel", this.tel);
          this.$router.push("/register/two");
        }
      });
    },
  },
};
</script>

<style>
.loginBtn {
  width: 80%;
  margin: 20px auto;
}
</style>